import { FFmpegTask } from './base'

/**
 * 水印位置
 */
export type WatermarkPosition =
  | 'top-left'
  | 'top-right'
  | 'bottom-left'
  | 'bottom-right'
  | 'center'

/**
 * 添加图片水印选项
 */
export interface AddImageWatermarkOptions {
  input: string
  output: string
  watermarkPath: string // 水印图片路径
  position?: WatermarkPosition
  opacity?: number // 透明度 0-1
  scale?: number // 缩放比例 0-1
  margin?: number // 边距(像素)
}

/**
 * 添加文字水印选项
 */
export interface AddTextWatermarkOptions {
  input: string
  output: string
  text: string
  position?: WatermarkPosition
  fontSize?: number
  fontColor?: string // 如 'white', '#ffffff'
  fontFile?: string // 字体文件路径
  opacity?: number
  margin?: number
}

/**
 * 去除水印选项
 */
export interface RemoveWatermarkOptions {
  input: string
  output: string
  x: number // 水印区域 X 坐标
  y: number // 水印区域 Y 坐标
  width: number // 水印区域宽度
  height: number // 水印区域高度
  method?: 'blur' | 'delogo' // 去除方法：模糊或去logo
  blurStrength?: number // 模糊强度 1-20
}

/**
 * 获取水印位置坐标
 */
function getWatermarkPosition(
  position: WatermarkPosition = 'bottom-right',
  margin: number = 10
): string {
  const positionMap: Record<WatermarkPosition, string> = {
    'top-left': `${margin}:${margin}`,
    'top-right': `W-w-${margin}:${margin}`,
    'bottom-left': `${margin}:H-h-${margin}`,
    'bottom-right': `W-w-${margin}:H-h-${margin}`,
    center: '(W-w)/2:(H-h)/2'
  }
  return positionMap[position]
}

/**
 * 添加图片水印
 */
export function addImageWatermark(options: AddImageWatermarkOptions): FFmpegTask {
  const position = getWatermarkPosition(options.position, options.margin)
  const opacity = options.opacity !== undefined ? options.opacity : 0.8
  const scale = options.scale !== undefined ? options.scale : 0.2

  // 构建滤镜
  const filters: string[] = []

  // 水印缩放和透明度
  filters.push(`[1:v]scale=iw*${scale}:ih*${scale},format=rgba,colorchannelmixer=aa=${opacity}[wm]`)

  // 叠加水印
  filters.push(`[0:v][wm]overlay=${position}`)

  const args: string[] = [
    '-i',
    options.input,
    '-i',
    options.watermarkPath,
    '-y',
    '-filter_complex',
    filters.join(';'),
    '-c:a',
    'copy', // 音频直接复制
    options.output
  ]

  return new FFmpegTask(args, options.input)
}

/**
 * 添加文字水印
 */
export function addTextWatermark(options: AddTextWatermarkOptions): FFmpegTask {
  const position = getWatermarkPosition(options.position, options.margin)
  const fontSize = options.fontSize || 24
  const fontColor = options.fontColor || 'white'
  const opacity = options.opacity !== undefined ? options.opacity : 0.8

  // 计算透明度对应的 alpha 值 (0-1 转 0-255)
  const alpha = Math.round(opacity * 255)
    .toString(16)
    .padStart(2, '0')

  let filter = `drawtext=text='${options.text}':fontsize=${fontSize}:fontcolor=${fontColor}@0x${alpha}:x=${position.split(':')[0]}:y=${position.split(':')[1]}`

  if (options.fontFile) {
    filter += `:fontfile='${options.fontFile}'`
  }

  const args: string[] = [
    '-i',
    options.input,
    '-y',
    '-vf',
    filter,
    '-c:a',
    'copy',
    options.output
  ]

  return new FFmpegTask(args, options.input)
}

/**
 * 去除水印
 */
export function removeWatermark(options: RemoveWatermarkOptions): FFmpegTask {
  let filter: string

  if (options.method === 'delogo') {
    // 使用 delogo 滤镜（效果更好但可能有边界）
    filter = `delogo=x=${options.x}:y=${options.y}:w=${options.width}:h=${options.height}`
  } else {
    // 使用模糊（默认方法）
    const blur = options.blurStrength || 10
    filter = `crop=${options.width}:${options.height}:${options.x}:${options.y},boxblur=${blur}:1[blurred];[0:v][blurred]overlay=${options.x}:${options.y}`
  }

  const args: string[] = [
    '-i',
    options.input,
    '-y',
    '-filter_complex',
    filter,
    '-c:a',
    'copy',
    options.output
  ]

  return new FFmpegTask(args, options.input)
}
